package com.grp6.game.chess;

import com.grp6.game.chess.model.ChessGameModel;
import com.grp6.game.chess.model.ChessPiece;
import com.grp6.game.chess.model.ChessPlace;
import com.grp6.game.chess.player.IChessPlayer;
import com.grp6.game.common.InvalidMoveException;
import com.grp6.game.controller.AbstractGameLifecycle;
import com.grp6.game.gameactions.IMove;
import com.grp6.game.player.IPlayer;

public class ChessGameLifecycle extends AbstractGameLifecycle {

	public ChessGameLifecycle(ChessGameModel model) {
		super(model);
	}

	@Override
	protected boolean hasGameFinished() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void execute(IMove move) {
		ChessMove cmove = (ChessMove) move;
		ChessPiece piece = cmove.getPiece();
		ChessPlace source = (ChessPlace) piece.getPlace();
		ChessPlace destination = cmove.getDestination();
		source.getPieces().remove(piece);
		destination.getPieces().clear();
		destination.getPieces().add(piece);
		piece.setPlace(destination);
		source.notifyObservers();
		destination.notifyObservers();
		piece.notifyObservers();
	}

	@Override
	protected void validate(IMove move) throws InvalidMoveException {
		// TODO Auto-generated method stub
	}

	
	private int turn = 1;
	@Override
	protected IPlayer getNextPlayer() {
		IChessPlayer player =(IChessPlayer) model.getPlayers().get(turn++%2);
		player.setPlaying(true);
		return player;
	}

}
